﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArbolandiaBL;
using Ext.Net;
using System.IO;
using System.Drawing;

public partial class catalogBanners : System.Web.UI.Page
{
    protected void Page_PreLoad(object sender, EventArgs e)
    {

        if (!ArbolandiaBL.Security.ValidLogin())
        {
            X.AddScript("this.parent.LogOut();");
        }
        if (!X.IsAjaxRequest)
        {
            dpfImageSizeCatHome.FieldLabel = ConstantsBL.SIZE_IMAGE_BANNER_HOME_TITLE;
            dpfImageSizeBottom.Text = ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_TITLE;
            dpfImageSizeMiddle.Text = ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_TITLE;
            dpfImageSizeCatTop.Text = ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_TITLE;
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            ReloadGrid(BannerType.Home);
            ReloadGrid(BannerType.Bottom);
            ReloadGrid(BannerType.Middle);
            ReloadGrid(BannerType.Top);
        }
    }
    protected void ReloadGrid(BannerType bt)
    {
        try
        {
            if (bt == BannerType.Home)
            {
                this.storeImagesHome.DataSource = new Service().GetBanners(bt);
                this.storeImagesHome.DataBind();
            }
            else if (bt == BannerType.Top)
            {
                this.storeImagesTop.DataSource = new Service().GetBanners(bt);
                this.storeImagesTop.DataBind();
            }
            else if (bt == BannerType.Middle)
            {
                this.storeImagesMiddle.DataSource = new Service().GetBanners(bt);
                this.storeImagesMiddle.DataBind();
            }
            else if (bt == BannerType.Bottom)
            {
                this.storeImagesBottom.DataSource = new Service().GetBanners(bt);
                this.storeImagesBottom.DataBind();
            }
        }
        catch (Exception ex)
        {
            //unexpected error
            X.Msg.Show(new MessageBoxConfig { Title = "Error", Message = ex.Message, Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.ERROR });
        }
    }
    protected void btnDeleteImage_Click(object sender, DirectEventArgs e)
    {
        int idbt, id = 0;
        int.TryParse(e.ExtraParams["idbt"], out idbt);
        BannerType bt = (BannerType)idbt;

        string json = e.ExtraParams["idValues"];

        if (string.IsNullOrEmpty(json))
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.WARNING,
                Title = "Alerta",
                Message = "Favor de Seleccionar un registro"
            });
            return;
        }
        Dictionary<string, string>[] valuesdic = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string sid = "";
        foreach (Dictionary<string, string> row in valuesdic)
        {
            sid = row["Id"];
        }

        Int32.TryParse(sid, out id);
        if (id <= 0)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.WARNING,
                Title = "Alerta",
                Message = "Favor de Seleccionar un registro"
            });
            return;
        }

        new Service().DeleteBanner(id, bt);
        ReloadGrid(bt);
    }
    protected void UploadClick(object sender, DirectEventArgs e)
    {

        try
        {
            string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
            if (this.uploadField.HasFile &&
                (this.uploadField.PostedFile.FileName.ToLower().Contains(".jpg"))
                )
            {
                int idbt;
                string linkUrl = e.ExtraParams["linkUrl"];
                int.TryParse(e.ExtraParams["idbt"], out idbt);
                BannerType bt = (BannerType)idbt;

                Guid pictureGuid = Guid.NewGuid();
                string extension = Path.GetExtension(this.uploadField.PostedFile.FileName);
                string pathfile = System.Configuration.ConfigurationManager.AppSettings["imagesfolder"];
                string filename = pictureGuid + extension;
                pathfile = Server.MapPath(@"~" + @"\" + pathfile + @"\" + filename);
                new Service().SaveRezizeImage(pathfile, uploadField.PostedFile.InputStream, ConstantsBL.SIZE_IMAGE_BANNER_HOME_HEIGHT, ConstantsBL.SIZE_IMAGE_BANNER_HOME_WIDTH);

                //Save Guid
                Picture pic = new Picture()
                {
                    IsFullSize = true,
                    Url = "http://hostimages/" + System.Configuration.ConfigurationManager.AppSettings["imagesfolder"] + "/" + filename,
                    ToolTip = "",
                    MimeType = "",
                    LinkUrl = linkUrl,
                    IsEnabled = true,
                    GuidId = pictureGuid,
                    GroupdId = 0
                };
                new Service().InsertBanner(pictureGuid, pic, bt);

                ReloadGrid(bt);
                pnlImageHome.Reset();
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "Exito",
                    Message = string.Format(tpl, this.uploadField.PostedFile.FileName, this.uploadField.PostedFile.ContentLength)
                });


            }
            else
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR,
                    Title = "Error",
                    Message = "No se pudo cargar el archivo"
                });
            }
        }
        catch (Exception ex)
        {

            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Error",
                Message = ex.Message
            });
            ArbolandiaBL.LogError.WriteError(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
    }
    protected void UploadClickTop(object sender, DirectEventArgs e)
    {
        string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
        if (this.uploadFieldTop.HasFile &&
            (this.uploadFieldTop.PostedFile.FileName.ToLower().Contains(".jpg"))
            )
        {
            int idbt;
            string linkUrl = e.ExtraParams["linkUrl"];
            int.TryParse(e.ExtraParams["idbt"], out idbt);
            BannerType bt = (BannerType)idbt;

            Guid pictureGuid = Guid.NewGuid();
            string extension = Path.GetExtension(this.uploadFieldTop.PostedFile.FileName);
            string pathfile = System.Configuration.ConfigurationManager.AppSettings["imagesfolder"];
            string filename = pictureGuid + extension;
            pathfile = Server.MapPath(@"~" + @"\" + pathfile + @"\" + filename);

            new Service().SaveRezizeImage(pathfile, uploadFieldTop.PostedFile.InputStream, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_HEIGHT, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_WIDTH);

            //Save Guid
            Picture pic = new Picture()
            {
                IsFullSize = true,
                Url = "http://hostimages/" + System.Configuration.ConfigurationManager.AppSettings["imagesfolder"] + "/" + filename,
                ToolTip = "",
                MimeType = "",
                LinkUrl = linkUrl,
                IsEnabled = true,
                GuidId = pictureGuid,
                GroupdId = 0
            };
            new Service().InsertBanner(pictureGuid, pic, bt);

            ReloadGrid(bt);
            pnlImageTop.Reset();
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Exito",
                Message = string.Format(tpl, this.uploadFieldTop.PostedFile.FileName, this.uploadFieldTop.PostedFile.ContentLength)
            });

        }
        else
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Error",
                Message = "No se pudo cargar el archivo. Seleccione un archivo valido. JPG"
            });
        }
    }
    protected void UploadClickMiddle(object sender, DirectEventArgs e)
    {
        string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
        if (this.uploadFieldMiddle.HasFile &&
            (
             this.uploadFieldMiddle.PostedFile.FileName.ToLower().Contains(".jpg") ||
            this.uploadFieldMiddle.PostedFile.FileName.ToLower().Contains(".jpeg"))
            )
        {
            int idbt;
            string linkUrl = e.ExtraParams["linkUrl"];
            int.TryParse(e.ExtraParams["idbt"], out idbt);
            BannerType bt = (BannerType)idbt;


            Guid pictureGuid = Guid.NewGuid();
            string extension = Path.GetExtension(this.uploadFieldMiddle.PostedFile.FileName);
            string pathfile = System.Configuration.ConfigurationManager.AppSettings["imagesfolder"];
            string filename = pictureGuid + extension;
            pathfile = Server.MapPath(@"~" + @"\" + pathfile + @"\" + filename);

            new Service().SaveRezizeImage(pathfile, uploadFieldMiddle.PostedFile.InputStream, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_HEIGHT, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_WIDTH);

            //Save Guid
            Picture pic = new Picture()
            {
                IsFullSize = true,
                Url = "http://hostimages/" + System.Configuration.ConfigurationManager.AppSettings["imagesfolder"] + "/" + filename,
                ToolTip = "",
                MimeType = "",
                LinkUrl = linkUrl,
                IsEnabled = true,
                GuidId = pictureGuid,
                GroupdId = 0
            };
            new Service().InsertBanner(pictureGuid, pic, bt);

            ReloadGrid(bt);
            pnlImageMiddle.Reset();
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Exito",
                Message = string.Format(tpl, this.uploadFieldMiddle.PostedFile.FileName, this.uploadFieldMiddle.PostedFile.ContentLength)
            });

        }
        else
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Error",
                Message = "No se pudo cargar el archivo"
            });
        }
    }
    protected void UploadClickBottom(object sender, DirectEventArgs e)
    {
        string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
        if (this.uploadFieldBottom.HasFile &&
            (this.uploadFieldBottom.PostedFile.FileName.ToLower().Contains(".jpg"))
            )
        {
            int idbt;
            string linkUrl = e.ExtraParams["linkUrl"];
            int.TryParse(e.ExtraParams["idbt"], out idbt);
            BannerType bt = (BannerType)idbt;


            Guid pictureGuid = Guid.NewGuid();
            string extension = Path.GetExtension(this.uploadFieldBottom.PostedFile.FileName);
            string pathfile = System.Configuration.ConfigurationManager.AppSettings["imagesfolder"];
            string filename = pictureGuid + extension;
            pathfile = Server.MapPath(@"~" + @"\" + pathfile + @"\" + filename);

            new Service().SaveRezizeImage(pathfile, uploadFieldBottom.PostedFile.InputStream, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_HEIGHT, ConstantsBL.SIZE_IMAGE_BANNER_RIGHT_WIDTH);

            //Save Guid
            Picture pic = new Picture()
            {
                IsFullSize = true,
                Url = "http://hostimages/" + System.Configuration.ConfigurationManager.AppSettings["imagesfolder"] + "/" + filename,
                ToolTip = "",
                MimeType = "",
                LinkUrl = linkUrl,
                IsEnabled = true,
                GuidId = pictureGuid,
                GroupdId = 0
            };
            new Service().InsertBanner(pictureGuid, pic, bt);

            ReloadGrid(bt);
            pnlImageBottom.Reset();
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Exito",
                Message = string.Format(tpl, this.uploadFieldBottom.PostedFile.FileName, this.uploadFieldBottom.PostedFile.ContentLength)
            });

        }
        else
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Error",
                Message = "No se pudo cargar el archivo"
            });
        }
    }
}